// pages/test/test.js
var Bmob = require('../../dist/Bmob-1.6.0.min.js');
var app = getApp()
const {
  $Message
} = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [],

    swiperCurrent: '0',
    current: '3',
    current_scroll: '3',
    array: [],
    contentarraylenghth: 0,

    colorArr: ["blue", "green", "red", "pink", "yellow"],
    randomColor: [],

  },
  listenSwiper: function(e) {
    //打印信息
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getLabelColor: function(k) {
    let that = this

    if (that.data.array == undefined) {
      return
    }
    // if (that.array[k] == undefined) {
    //   var ran = this.data.randomColor
    //   ran.push([])
    //   this.setData({
    //     randomColor: ran
    //   })
    //   return;
    // }
    var colorLen = this.data.colorArr.length;
    var arrLen = this.data.array[k].length;
    var colorArr = this.data.colorArr;
    var newArr = [];
    var newcolorArr = [];
    var pre = null;
    for (var i = 0; i < arrLen; i++) {
      newcolorArr = [];
      for (var j = 0; j < that.data.array[k][i].tag.length; j++) {
        while (random == pre) {
          var random = colorArr[Math.floor(Math.random() * colorLen)];
        }
        pre = random;
        newcolorArr.push(random);
      }

      newArr.push(newcolorArr);
    }
    var ran = this.data.randomColor

    ran[k] = newArr
    this.setData({
      randomColor: ran
    })
  },


  onLoad: function(options) {
    var that = this;
    if (options.id == "1") {
        that.setData({
          tabs: ['学习', '研习空间']
        })

        wx.setNavigationBarTitle({
          title: '学习'
        });
    } else if (options.id == "2") {
      that.setData({
        tabs: ['锻炼','羽毛球', '篮球', '乒乓球']
      })

      wx.setNavigationBarTitle({
        title: '锻炼'
      });

    } else if (options.id == "3") {
      that.setData({
        tabs: ['娱乐', '约饭', '电影']
      })
      wx.setNavigationBarTitle({
        title: '娱乐'
      });

    }
    that.setData({
      array: new Array(that.data.tabs.length)
    })
    this.setData({
      contentarraylenghth: 4 * 40
    })
    console.log("现在的页面高度是：", that.data.contentarraylenghth)

    // that.getLabelColor()

    //去数据库把帖子都拿出来 顺便把帖子的作者拿出来
    var temp = {}
    var array = new Array(3)
    //查找帖子数据
    for (let k = 0; k < that.data.tabs.length; k++) {
      var item = that.data.tabs[k]
      var query = Bmob.Query("Postings");
      query.statTo("where", '{"tag":{"$inQuery":{"where":{"tag":"' + item + '"},"className":"label"}}}');
      query.include('user', 'post')
      // query.descending("createdAt")
      query.order('-createdAt');
      query.find().then(res => {
        // 得到对应tag的数据
        var list = []
        for (let i = 0; i < res.length; i++) {
          var label_list;
          //查找tags
          const query1 = Bmob.Query("label")

          query1.statTo("where", '{"$relatedTo":{"object":{"__type":"Pointer","className":"Postings","objectId":"' + res[i].objectId + '"},"key":"tag"}}');

          // query1.statTo("where", '{"tag":{"$inQuery":{"where":{"tag":"乒乓球"},"className":"label"}}}');
          query1.find().then(res1 => {
            //查找UserData
            const pointer = Bmob.Pointer('_User')
            const poiID = pointer.set(res[i].user.objectId)

            const query = Bmob.Query('UserData')
            query.include('major', 'post')
            query.equalTo("user", "==", poiID);
            query.find().then(res2 => {

              label_list = []
              for (var j = 0; j < res1.length; j++) {
                label_list.push(res1[j].tag);
              }
              //把帖子的数据存入list
              if (res2.length) {
                //如果服务器有UserData
                temp = {
                  objectid: res[i].objectId,
                  title: res[i].title,
                  user: res[i].user,
                  name: res2[0].nickName,
                  imageurl: res2[0].userPic.url,
                  content: res[i].detail,
                  tag: label_list
                }
              } else {
                temp = {
                  //如果服务器没有UserData
                  objectid: res[i].objectId,
                  title: res[i].title,
                  user: res[i].user,
                  name: res[i].user.nickName,
                  imageurl: res[i].user.userPic,
                  content: res[i].detail,
                  tag: label_list
                }
              }
              array = that.data.array
              list.push(temp)
              array[k] = list
              that.setData({
                array: array
              })
              that.getLabelColor(k)
            })
            // console.log(that.data.array)
          }).catch(err => {
            console.log(err)
          })
        }

        // var array = that.data.array
        // array.push(list)


      }).catch(err => {
        $Message({
          content: '获取服务器信息失败',
          type: 'error'
        });
        console.log(err)
      })
    }




  },
  swiperchange: function(e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  handleChange({
    detail
  }) {
    this.setData({
      current: detail.key,
    });
    console.log('handleChange', this.data);
  },
  handleChangeScroll({
    detail
  }) {
    this.setData({
      swiperCurrent: detail.key
    });
    console.log('handleChangeScroll', detail.key);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})